package com.berwin.syncer;

import android.app.AlertDialog;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.berwin.utility.UserDefault;

public class ConnectDialog extends DialogFragment implements View.OnClickListener {

    public interface ConnectInfoListener {
        void onConnectInfoChanged(boolean useScanner, String host, int portText, int portFile);
    }

    public static final String KEY_HOST = "key_host";
    public static final String KEY_PORT_TEXT = "key_port_text";
    public static final String KEY_PORT_FILE = "key_port_file";

    public static EditText etHost;
    public static EditText etPortText;
    public static EditText etPortFile;

    public static Button btnConnect;

    public ImageView ivScanner;
    public ImageView ivClose;

    private ConnectInfoListener listener;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        listener = (ConnectInfoListener) getActivity();
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_login, null);

        etHost = view.findViewById(R.id.et_host);
        etPortText = view.findViewById(R.id.et_port_text);
        etPortFile = view.findViewById(R.id.et_port_file);

        btnConnect = view.findViewById(R.id.btn_connect);
        ivScanner = view.findViewById(R.id.iv_scanner);

        ivClose = view.findViewById(R.id.iv_close);

        btnConnect.setOnClickListener(this);
        ivScanner.setOnClickListener(this);
        ivClose.setOnClickListener(this);

        etHost.setText(UserDefault.getInstance().getValueForKey(KEY_HOST, "192.168."));
        etPortText.setText(UserDefault.getInstance().getValueForKey(KEY_PORT_TEXT, 11098) + "");
        etPortFile.setText(UserDefault.getInstance().getValueForKey(KEY_PORT_FILE, 11099) + "");

        builder.setView(view);
        return builder.create();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_connect:
                String host = etHost.getText().toString().trim();
                String portText = etPortText.getText().toString().trim();
                String portFile = etPortFile.getText().toString().trim();
                UserDefault.getInstance().setValueForKey(KEY_HOST, host);
                UserDefault.getInstance().setValueForKey(KEY_PORT_TEXT, Integer.parseInt(portText));
                UserDefault.getInstance().setValueForKey(KEY_PORT_FILE, Integer.parseInt(portFile));
                listener.onConnectInfoChanged(false, host, Integer.parseInt(portText), Integer.parseInt(portFile));
                break;
            case R.id.iv_scanner:
                // 使用扫码登录返回空
                listener.onConnectInfoChanged(true, null, -1, -1);
                break;
            case R.id.iv_close:
            default:
                break;
        }
        dismiss();
    }
}
